//
//  Device.m
//  Shion
//
//  Created by Chris Karr on 12/17/07.
//  Copyright 2007-2008 Audacious Software. All rights reserved.
//

#import "Device.h"
#import "DeviceManager.h"

@implementation Device

@synthesize name;
@synthesize kind;
@synthesize address;
@synthesize dimmer;
@synthesize actualStatus;
@synthesize todoStatus;
@synthesize visibleStatus;

- (Device *) init
{
	if (self = [super init])
	{
		unsigned char bytes[] = {0x00,0x00, 0x00};
		self.address = [NSMutableData dataWithBytes:bytes length:3];
		self.name = @"New device";
		self.kind = INSTEON_APPLIANCE_LINC;
		
		self.actualStatus = [[DeviceStatus alloc] init];
		self.todoStatus = [[DeviceStatus alloc] init];
		self.visibleStatus = [[DeviceStatus alloc] init];

		self.visibleStatus.lightLevel = 0;

		[self.actualStatus addObserver:self forKeyPath:@"lightLevel" options:0 context:NULL];
		[self.todoStatus addObserver:self forKeyPath:@"lightLevel" options:0 context:NULL];
		[self.visibleStatus addObserver:self forKeyPath:@"lightLevel" options:0 context:NULL];

	}

	return self;
}

- (void) observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
	if (object == self.visibleStatus)
	{
		self.todoStatus.lightLevel = self.visibleStatus.lightLevel;
		[[NSNotificationCenter defaultCenter] postNotificationName:SHION_SYNCNOW object:self];
	}
	else if (object == self.actualStatus)
	{
		[self willChangeValueForKey:@"active"];
		self.visibleStatus.lightLevel = self.actualStatus.lightLevel;
		self.todoStatus.lightLevel = nil;
		[self didChangeValueForKey:@"active"];
	}
}

- (void) pingStatus
{
	NSMutableDictionary * action = [NSMutableDictionary dictionary];
	[action setValue:@"query" forKey:@"action"];
	[action setValue:self.address forKey:@"address"];
	
	[[NSNotificationCenter defaultCenter]  postNotificationName:SHION_INSTRUCTION object:action];
}

- (NSString *) addressString 
{
	NSMutableString * string = [[NSMutableString alloc] init];
	
	const unsigned char * bytes = [self.address bytes];
		
	int i = 0;
	for (i = 0; i < [self.address length]; i++)
		[string appendString:[NSString stringWithFormat:@"%02x", bytes[i]]];

	return string;
}
 
- (void) setAddressString:(NSString *) newAddress 
{
	if (newAddress == nil || [newAddress isEqual:@""] || [newAddress length] != 6)
		return;
	
	NSString * a = [newAddress substringWithRange:NSMakeRange (0, 2)];
	NSString * b = [newAddress substringWithRange:NSMakeRange (2, 2)];
	NSString * c = [newAddress substringWithRange:NSMakeRange (4, 2)];
		
	unsigned char aHex = (unsigned char) strtol ([a cStringUsingEncoding:NSASCIIStringEncoding], nil, 0x10);
	unsigned char bHex = (unsigned char) strtol ([b cStringUsingEncoding:NSASCIIStringEncoding], nil, 0x10);
	unsigned char cHex = (unsigned char) strtol ([c cStringUsingEncoding:NSASCIIStringEncoding], nil, 0x10);
	
	unsigned char addressBytes[] = {aHex, bHex, cHex};

	self.address = [NSData dataWithBytes:addressBytes length:3];
}

- (NSNumber *) active
{
	if ([self.actualStatus.lightLevel integerValue] == 0)
		return [NSNumber numberWithBool:NO];
	else
		return [NSNumber numberWithBool:YES];
}

- (void) setActive:(NSNumber *) isActive
{
	if ([isActive boolValue])
		self.visibleStatus.lightLevel = [NSNumber numberWithInteger:255];
	else
		self.visibleStatus.lightLevel = [NSNumber numberWithInteger:0];
}

@end
